package com.csdn.leon.anima;

import java.util.ArrayList;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

public class AnimatorUtil {
	
	static AnimatorSet backAnimatorSet;
	static AnimatorSet hideAnimatorSet;
	static Animation rotateAnimator;

	public static void animateBack(ImageView floatBtn) {
		if (hideAnimatorSet != null && hideAnimatorSet.isRunning()) {
			hideAnimatorSet.cancel();
		}
		if (backAnimatorSet != null && backAnimatorSet.isRunning()) {

		} else {
			backAnimatorSet = new AnimatorSet();
			// ObjectAnimator headerAnimator = ObjectAnimator.ofFloat(toolbar,
			// "translationY", toolbar.getTranslationY(), 0f);
			ObjectAnimator footerAnimator = ObjectAnimator.ofFloat(floatBtn,
					"translationY", floatBtn.getTranslationY(), 0f);
			ArrayList<Animator> animators = new ArrayList<>();
			// animators.add(headerAnimator);
			animators.add(footerAnimator);
			backAnimatorSet.setDuration(100);
			backAnimatorSet.playTogether(animators);
			backAnimatorSet.start();
		}
	}



	public static void animateHide(ImageView floatBtn) {
		if (backAnimatorSet != null && backAnimatorSet.isRunning()) {
			backAnimatorSet.cancel();
		}
		if (hideAnimatorSet != null && hideAnimatorSet.isRunning()) {

		} else {
			hideAnimatorSet = new AnimatorSet();
			// ObjectAnimator headerAnimator = ObjectAnimator.ofFloat(toolbar,
			// "translationY", toolbar.getTranslationY(), -toolbar.getHeight());
			ObjectAnimator footerAnimator = ObjectAnimator.ofFloat(floatBtn,
					"translationY", floatBtn.getTranslationY(),
					floatBtn.getHeight() + 10);
			ArrayList<Animator> animators = new ArrayList<>();
			// animators.add(headerAnimator);
			animators.add(footerAnimator);
			hideAnimatorSet.setDuration(100);
			hideAnimatorSet.playTogether(animators);
			hideAnimatorSet.start();
		}
		
		
	}

	public static RotateAnimation getRotateAnimator(){
		RotateAnimation rotateAnimator = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);  
	
		rotateAnimator.setInterpolator(new LinearInterpolator());//不停顿  
		rotateAnimator.setRepeatCount(1000);//重复次数  
		rotateAnimator.setFillAfter(true);//停在最后  
		rotateAnimator.setDuration(500);  
		rotateAnimator.setAnimationListener(new AnimationListener() {  
	            @Override  
	            public void onAnimationStart(Animation animation) {  
	            }  
	            @Override  
	            public void onAnimationRepeat(Animation animation) {  
	            }  
	            @Override  
	            public void onAnimationEnd(Animation animation) {  
	            }  
	        });  
		return rotateAnimator;
	}
}
